//
//  CalendarViewController.swift
//  BaseSwift
//
//  Created by ghwang on 2019/8/6.
//  Copyright © 2019 ghwang. All rights reserved.
//

import UIKit

class CalendarViewController: UIViewController {

    var dateClick:((StockM) -> Void)?
    var prices:[String:StockM]? = nil
    var curSubIndex = 0
    let priceLab:UILabel = UILabel()
    var product:ProductVo?
    var dayPrices:Array<StockM> = Array()
    var curStock:StockM?
    var curProduct:ProductProList?
    let audLab = UILabel()
    let childLab = UILabel()
    let childNumLab = UILabel()
    let audNumLab = UILabel()
    var num1 = 1
    var num2 = 0
    let childMinusBtn = UIButton()


    
    
    private var calendar:FSCalendar? = nil
    private var dateFormatter: DateFormatter?
    private var tempStocks: [String:StockM] = Dictionary<String,StockM>()
    
    func getDayPrice(){
        

        let defStartDay:String = NSDate.br_getString(Date(), format: "yyyy-MM-dd")!
        let defEndDay:String = NSDate.br_getString(NSDate.init(timeInterval: 90*24*3600, since: Date()) as Date, format: "yyyy-MM-dd")!
        
        API.productDayPriceList(params: ["id":self.curProduct!.pdtId!,
                                         "proId":self.curProduct!.id!,
                                         "beginDate":defStartDay,
                                         "endDate":defEndDay]) { (result) in
            
            

                                            
                                            self.dayPrices = result as! Array<StockM>
                                            
                                            if self.dayPrices.count == 0{
                                                
                                                self.payBtn.isEnabled = false
                                                self.payBtn.backgroundColor = UIColor.init(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.2)
                                                ToastUtil.show(msg: "暂无日期价格")
                                                self.tempStocks.removeAll()
                                                self.calendar?.reloadData()

                                                return
                                            }
                                            self.payBtn.isEnabled = true
                                            self.payBtn.backgroundColor = UIColor.btnMainColor
                                            let lab = self.payBtn.viewWithTag(9898)
                                            self.payBtn.bringSubviewToFront(lab!)
                                            

                                    
                                            var dic = Dictionary<String,StockM>()
                                            
                                            for item in self.dayPrices{
                                                
                                                let date = item.saleDate?.split(string: "T")[0]

                                                dic[date!] = item
                                            }
                                            self.prices = dic
                                            self.curStock = self.dayPrices.first
                                            self.tempStocks.removeAll()
                                            self.updatePrice()

                                            for item in self.dayPrices{

                                                item.markPrice = String(format: "￥%.2f", item.adultPrice)
                                                let date = item.saleDate?.split(string: "T")[0]
                                                self.tempStocks[date!] = item
                                            }
                                            self.calendar?.reloadData()
                                            
                                                    
        }
    }
    let payBtn = UIButton()
    lazy var bottomView:UIView = {
        
        let bg = UIView()
        bg.backgroundColor = UIColor.white
        
    
        bg.addSubview(payBtn)
        payBtn.addTarget(self, action: #selector(sureClick), for: .touchUpInside)
        payBtn.sd_cornerRadius = LARAGE_BTN_RADIUS
        let _ = payBtn.sd_layout()?.widthIs(164)?.heightIs(LARAGE_BTN_HEIGHT)?.rightSpaceToView(bg,10)?.centerYEqualToView(bg)
        
        let tlab = UILabel()
        tlab.text = "确定"
        tlab.tag = 9898
        tlab.font = UIFont.commonFontSmall
        tlab.textColor = UIColor.white
        tlab.textAlignment = .center
        payBtn.addSubview(tlab)
        let _ = tlab.sd_layout()?.spaceToSuperView(UIEdgeInsets.zero)
        
        let priceBtn = UIButton()
        priceBtn.setTitle("总价:", for: .normal)
        priceBtn.titleLabel?.font = UIFont.commonFont
        priceBtn.setTitleColor(UIColor.tabbarDefaultTextColor, for: .normal)
        priceBtn.addTarget(self, action: #selector(priceInfoClick), for: .touchUpInside)
        bg.addSubview(priceBtn)
        let _ = priceBtn.sd_layout()?.leftSpaceToView(bg,16)?.centerYEqualToView(bg)!.widthIs(125)!.heightIs(35)
        let _ =  priceBtn.titleLabel?.sd_layout()?.leftSpaceToView(priceBtn,0)!.heightIs(20)!.widthIs(45)!.centerYEqualToView(priceBtn)
        
        priceBtn.addSubview(self.priceLab)
        self.priceLab.font = UIFont.commonFont
        self.priceLab.textColor = UIColor.GP_Orange_Color
        let _  =  self.priceLab.sd_layout()?.leftSpaceToView(priceBtn,40)!.heightIs(20)!.centerYEqualToView(priceBtn)
        self.priceLab.setSingleLineAutoResizeWithMaxWidth(155)
    
        let icon = UIImageView()
        icon.image = UIImage(named: "向下")
        priceBtn.addSubview(icon)
        let _  =  icon.sd_layout()?.widthIs(16)!.heightIs(16)!.leftSpaceToView(self.priceLab,3)!.bottomSpaceToView(priceBtn,8)
        
    
        
        return bg
        
    }()
    
    override func loadView() {
        
        let view = UIView.init(frame: UIScreen.main.bounds)
        view.backgroundColor = UIColor.backgroundColor
        self.view = view

        self.updatePrice()
        let subView = UIView()
        subView.sd_cornerRadius = 12
        subView.backgroundColor = UIColor.white
        view.addSubview(subView)
        let _ = subView.sd_layout()?.leftSpaceToView(view,16)?.rightSpaceToView(view,16)!.topSpaceToView(view,HT_StatusBarAndNavigationBarHeight+16)?.autoHeightRatio(0)
        
        
        let ttLab = UILabel()
        ttLab.backgroundColor = UIColor.white
        ttLab.textColor = UIColor.textTitleColorBlack
        ttLab.font = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight(rawValue: 0.2))
        subView.addSubview(ttLab)
        let _ = ttLab.sd_layout()?.topSpaceToView(subView,16)?.leftSpaceToView(subView,16)?.heightIs(25)!.widthIs(95)
        ttLab.text = "选择套餐"
        
        let tags = NSMutableArray()
        for item in self.product!.productProList!{
            
            tags.add(item.pdtName as Any)
        }

        let tagsView = STTagsView.tagView(withFrame: CGRect(x: 0, y: 35, width: KScreenWidth-32, height: 0), tagsArray:(tags as! [Any]), textColor: UIColor.textTitleColorBlack, textFont: UIFont .smallerFont, normalTagBackgroundColor: UIColor.backgroundColor, tagBorderColor: UIColor.lineColor, contentInsets: UIEdgeInsets(top: 20, left: 16, bottom: 20, right: 16), labelContentInsets: UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10), labelHorizontalSpacing: 5, labelVerticalSpacing: 5)
        
        tagsView?.tagClickEnable = true
        tagsView?.indexSelectedBgColor = 0
        tagsView?.tagSelectBackgroundColor = UIColor.init(hexString: "#fef0e5")
        tagsView?.indexSelectedBgColor = Int32(self.curSubIndex)
        tagsView?.selectBorderColor = UIColor.btnMainColor
        tagsView?.backgroundColor = UIColor.white
        tagsView?.setBorderGreenColor()
        subView.addSubview(tagsView!)
        
        tagsView?.tagClick = {(index) -> Void in
            

            self.curSubIndex = index
            self.curProduct = self.product?.productProList!.get(at: index)
            self.getDayPrice()
            
            
            
        }
        subView.setupAutoHeight(withBottomView: tagsView, bottomMargin: 0)
        
        
        let bgScr = UIScrollView()
        view.addSubview(bgScr)
        let _ = bgScr.sd_layout()?.topSpaceToView(subView,10)!.widthRatioToView(view,1)!.bottomSpaceToView(view,HT_TabbarSafeBottomMargin+64)
        
        
        
        var dic = Dictionary<String,StockM>()

        for item in self.dayPrices{


            item.markPrice = String(format: "￥%.2f", item.adultPrice)
            let date = item.saleDate?.split(string: "T")[0]
            self.tempStocks[date!] = item
            
            dic[date!] = item

            
        }
        self.prices = dic
        
        calendar = FSCalendar()
        calendar?.locale = NSLocale(localeIdentifier: "zh_CN") as Locale
        bgScr.addSubview(calendar!)
        calendar?.sd_cornerRadius = 12
        calendar?.dataSource = self
        calendar?.delegate = self
        let _ = calendar?.sd_layout()?.leftSpaceToView(bgScr,16)!.rightSpaceToView(bgScr,16)!.heightIs(280)!.topSpaceToView(bgScr,0)
        calendar?.appearance.subtitleFont = UIFont.systemFont(ofSize: 10)
        calendar?.appearance.headerTitleFont = UIFont.commonFontSmall
        calendar?.appearance.borderRadius = 0
        calendar?.appearance.selectionColor = UIColor.init(hexString: "#ffede0")
        calendar?.appearance.subtitleDefaultColor = UIColor.textPriceColor
        calendar?.appearance.headerTitleColor = UIColor.btnMainColor
        calendar?.appearance.weekdayTextColor = UIColor.btnMainColor
        calendar?.appearance.subtitleSelectionColor = UIColor.textPriceColor
        calendar?.appearance.titleSelectionColor = UIColor.textPriceColor
        calendar?.scrollDirection = .horizontal
        calendar?.backgroundColor = UIColor.white
        calendar?.layer.borderColor = UIColor.clear.cgColor
        calendar?.layer.borderWidth = 0
        
        let numView =  UIView()
        numView.sd_cornerRadius = 12
        numView.backgroundColor = UIColor.white
        bgScr.addSubview(numView)
        let _ = numView.sd_layout()?.leftSpaceToView(bgScr,16)!.rightSpaceToView(bgScr,16)!.heightIs(125)!.topSpaceToView(calendar,10)
        
        let ttLab0 = UILabel()
        ttLab0.backgroundColor = UIColor.white
        ttLab0.textColor = UIColor.textTitleColorBlack
        ttLab0.font = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight(rawValue: 0.2))
        numView.addSubview(ttLab0)
        let _ = ttLab0.sd_layout()?.topSpaceToView(numView,0)?.leftSpaceToView(numView,16)?.heightIs(25)!.widthIs(95)
        ttLab0.text = "出行人数"
        
        //成人
        let audiltView = UIView()
        numView.addSubview(audiltView)
        let _ = audiltView.sd_layout()?.widthRatioToView(numView,1)!.heightIs(50)!.topSpaceToView(ttLab0,0)
        audLab.font = UIFont.commonFont
        audLab.textColor =  UIColor.tabbarDefaultTextColor
        audiltView.addSubview(audLab)
        let _ = audLab.sd_layout()?.leftSpaceToView(audiltView,16)!.heightIs(20)!.centerYEqualToView(audiltView)!.rightSpaceToView(audiltView,155)
        
        let audAddBtn = UIButton()
        audAddBtn.addTarget(self, action: #selector(audAddClick), for: .touchUpInside)
        audAddBtn.setImage(UIImage(named: "numAdd"), for: .normal)
        audiltView.addSubview(audAddBtn)
        let _ = audAddBtn.sd_layout()?.widthIs(35)!.heightIs(35)!.centerYEqualToView(audiltView)!.rightSpaceToView(audiltView,16)
        
        audNumLab.text  = "1"
        audNumLab.textAlignment = .center
        audNumLab.font = UIFont.commonFont
        audNumLab.textColor =  UIColor.tabbarDefaultTextColor
        audiltView.addSubview(audNumLab)
        let _ = audNumLab.sd_layout()?.rightSpaceToView(audAddBtn,5)!.heightIs(20)!.centerYEqualToView(audiltView)!.widthIs(45)
        
        let audMinusBtn = UIButton()
        audMinusBtn.addTarget(self, action: #selector(audMinusClick), for: .touchUpInside)
        audMinusBtn.setImage(UIImage(named: "numMinus"), for: .normal)
        audiltView.addSubview(audMinusBtn)
        let _ = audMinusBtn.sd_layout()?.widthIs(35)!.heightIs(35)!.centerYEqualToView(audiltView)!.rightSpaceToView(audNumLab,5)
        
        
        
        
        
        
        //儿童
        let childView = UIView()
        numView.addSubview(childView)
        let _ = childView.sd_layout()?.widthRatioToView(numView,1)!.heightIs(50)!.topSpaceToView(audiltView,0)
        childLab.font = UIFont.commonFont
        childLab.textColor =  UIColor.tabbarDefaultTextColor
        childView.addSubview(childLab)
        let _ = childLab.sd_layout()?.leftSpaceToView(childView,16)!.heightIs(20)!.centerYEqualToView(childView)!.rightSpaceToView(childView,155)
        
        
        
        let childAddBtn = UIButton()
        childAddBtn.addTarget(self, action: #selector(childAddClick), for: .touchUpInside)
        childAddBtn.setImage(UIImage(named: "numAdd"), for: .normal)
        childView.addSubview(childAddBtn)
        let _ = childAddBtn.sd_layout()?.widthIs(35)!.heightIs(35)!.centerYEqualToView(childView)!.rightSpaceToView(childView,16)
        
        childNumLab.text  = "0"
        childNumLab.isHidden = true
        childNumLab.textAlignment = .center
        childNumLab.font = UIFont.commonFont
        childNumLab.textColor =  UIColor.tabbarDefaultTextColor
        childView.addSubview(childNumLab)
        let _ = childNumLab.sd_layout()?.rightSpaceToView(childAddBtn,5)!.heightIs(20)!.centerYEqualToView(childView)!.widthIs(45)
        
        childMinusBtn.isHidden = true
        childMinusBtn.addTarget(self, action: #selector(childMinusClick), for: .touchUpInside)

        childMinusBtn.setImage(UIImage(named: "numMinus"), for: .normal)
        childView.addSubview(childMinusBtn)
        let _ = childMinusBtn.sd_layout()?.widthIs(35)!.heightIs(35)!.centerYEqualToView(childView)!.rightSpaceToView(childNumLab,5)
        
        
        bgScr.setupAutoContentSize(withBottomView: numView, bottomMargin: 10)
        
        
        
        
        view.addSubview(self.bottomView)
        let _ = self.bottomView.sd_layout()?.widthRatioToView(view,1)!.bottomSpaceToView(view,HT_TabbarSafeBottomMargin)!.heightIs(64)
        
        
        
        
        
    }
    @objc func audAddClick(){
        
        self.num1 = self.num1+1
        self.audNumLab.text = self.num1.description
        self.updatePrice()
    }
    @objc func audMinusClick(){
        
        self.num1 = self.num1-1
        if self.num1 < 0 {
            
            self.num1 = 0
        }
        self.audNumLab.text = self.num1.description
        self.updatePrice()
        
    }
    @objc func childAddClick(){
        
        self.num2 = self.num2+1
        self.childNumLab.text = self.num2.description
        self.updatePrice()
        childNumLab.isHidden = false
        childMinusBtn.isHidden = false
    }
    @objc func childMinusClick(){
        
        self.num2 = self.num2-1
        if self.num2 <= 0 {
            
            self.num2 = 0
            childNumLab.isHidden = true
            childMinusBtn.isHidden = true
        }
        self.childNumLab.text = self.num2.description
        self.updatePrice()
        
    }
    
    func updatePrice(){
        
        if self.curStock == nil {
            
            self.audLab.text  = "成人    ￥0.00"
            self.childLab.text  = "儿童     ￥0.00"
            
            self.priceLab.text = String(format: "￥0.00")
            return
        }
        
        self.audLab.text  = "成人    ￥\(self.curStock?.adultPrice ?? 0.00)"
        self.childLab.text  = "儿童     ￥\(self.curStock?.childPrice ?? 0.00)"
        
        let sum1 = self.curStock!.adultPrice * Float(self.num1)
        let sum2 = self.curStock!.childPrice * Float(self.num2)

        self.priceLab.text = String(format: "￥%.2f", sum1+sum2)

        
    }
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.view.backgroundColor = UIColor.backgroundColor
        self.navigationView.setTitle("选择类型/日期/人数")
        
        
        self.dateFormatter = DateFormatter()
        self.dateFormatter?.timeZone = TimeZone(identifier: "Asia/Shanghai")
        self.dateFormatter?.dateFormat = "yyyy-MM-dd"
        self.getDayPrice()
        
    }
    
    @objc func sureClick(){
        
        
        

        let wrvc = WriteViewController()
        
        if self.dayPrices.count > 0 && self.curStock == nil {
            
            wrvc.stock = self.dayPrices.first
            self.curStock = self.dayPrices.first
            
        }else{
            
            wrvc.stock = self.curStock

        }
        
        let priceInfo = String(format: "%@\n%@", "成人:￥\(self.curStock?.adultPrice ?? 0.00)*\(self.num1)","儿童:￥\(self.curStock?.childPrice ?? 0.00)*\(self.num2)")
        wrvc.priceInfo = priceInfo

        
        wrvc.subPro = self.curProduct
        wrvc.product = self.product
        wrvc.allPrice = priceLab.text
        wrvc.num1 = self.num1
        wrvc.num2 = self.num2
        PUSH(from: self, to: wrvc)
        
    }    
    let pricebg = UIView()
    @objc func priceInfoClick() {
        
        if self.curStock == nil {
            
            ToastUtil.show(msg: "暂无价格")
            return
        }
        
         self.view.addSubview(pricebg)
         let _  = pricebg.sd_layout()?.spaceToSuperView(.zero)

         let bgImgv = UIImageView()
         bgImgv.alpha = 0.5
         bgImgv.backgroundColor = UIColor.init(patternImage: UIImage(named: "paybg")!)
         pricebg.addSubview(bgImgv)
         let _ = bgImgv.sd_layout()?.spaceToSuperView(.zero)
         
         let bg2 = UIView()
         bg2.backgroundColor = UIColor.white
         pricebg.addSubview(bg2)
         let _ = bg2.sd_layout()?.bottomSpaceToView(pricebg,HT_TabbarSafeBottomMargin)?.widthRatioToView(pricebg,1)?.heightIs(195)
        
        
        
        
        
        let bg = UIView()
        bg2.addSubview(bg)
        let _ = bg.sd_layout()?.heightIs(64)!.widthRatioToView(bg2,1)!.bottomSpaceToView(bg2,0)
        let line = UIView()
        line.backgroundColor = UIColor.lineColor
        bg.addSubview(line)
        let _ = line.sd_layout()?.topSpaceToView(bg,0)!.heightIs(1)!.widthRatioToView(bg,1)
        
        let titlab = UILabel()
        titlab.text = "费用明细"
        titlab.font = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight(rawValue: 0.2))
        titlab.textColor = UIColor.black
        titlab.textAlignment = .center
        bg2.addSubview(titlab)
        let _  = titlab.sd_layout()?.centerXEqualToView(bg2)!.heightIs(20)!.topSpaceToView(bg2,16)!.widthIs(95)
        
        
        let aduLab = UILabel()
        aduLab.text = "成人"
        aduLab.font = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight(rawValue: 0.2))
        aduLab.textColor = UIColor.init(hexString: "#999999")
        bg2.addSubview(aduLab)
        let _  = aduLab.sd_layout()?.leftSpaceToView(bg2,56)!.heightIs(20)!.topSpaceToView(bg2,60)!.widthIs(45)
        
        let aduValueLab = UILabel()
        aduValueLab.text = String(format: "￥%.2f x%d", self.curStock?.adultPrice ?? 0.00,self.num1)
        aduValueLab.font = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight(rawValue: 0.2))
        aduValueLab.textColor = UIColor.init(hexString: "#999999")
        bg2.addSubview(aduValueLab)
        let _  = aduValueLab.sd_layout()?.rightSpaceToView(bg2,56)!.heightIs(20)!.topSpaceToView(bg2,60)!
        aduValueLab.setSingleLineAutoResizeWithMaxWidth(166)
        
        let childLab = UILabel()
        childLab.text = "儿童"
        childLab.font = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight(rawValue: 0.2))
        childLab.textColor = UIColor.init(hexString: "#999999")
        bg2.addSubview(childLab)
        let _  = childLab.sd_layout()?.leftSpaceToView(bg2,56)!.heightIs(20)!.topSpaceToView(aduLab,16)!.widthIs(45)
        
        let childValueLab = UILabel()
        childValueLab.text = String(format: "￥%.2f x%d", self.curStock?.childPrice ?? 0.00,self.num2)
        childValueLab.font = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight(rawValue: 0.2))
        childValueLab.textColor = UIColor.init(hexString: "#999999")
        bg2.addSubview(childValueLab)
        let _  = childValueLab.sd_layout()?.rightSpaceToView(bg2,56)!.heightIs(20)!.topSpaceToView(aduLab,16)!
        childValueLab.setSingleLineAutoResizeWithMaxWidth(166)
        
        if self.num2 == 0 {
            
            childLab.isHidden = true
            childValueLab.isHidden = true
        }
        

        
        let payBtn = UIButton()
       bg.addSubview(payBtn)
       payBtn.addTarget(self, action: #selector(sureClick), for: .touchUpInside)
       payBtn.sd_cornerRadius = LARAGE_BTN_RADIUS
       let _ = payBtn.sd_layout()?.widthIs(164)?.heightIs(LARAGE_BTN_HEIGHT)?.rightSpaceToView(bg,10)?.centerYEqualToView(bg)
       payBtn.gradientBtnMainColor()
       
       let tlab = UILabel()
       tlab.text = "确定"
       tlab.font = UIFont.commonFontSmall
       tlab.textColor = UIColor.white
       tlab.textAlignment = .center
       payBtn.addSubview(tlab)
       let _ = tlab.sd_layout()?.spaceToSuperView(UIEdgeInsets.zero)
       
       let priceBtn = UIButton()
       priceBtn.setTitle("总价:", for: .normal)
       priceBtn.titleLabel?.font = UIFont.commonFont
       priceBtn.setTitleColor(UIColor.tabbarDefaultTextColor, for: .normal)
       priceBtn.addTarget(self, action: #selector(infoHideClick), for: .touchUpInside)
       bg.addSubview(priceBtn)
       let _ = priceBtn.sd_layout()?.leftSpaceToView(bg,16)?.centerYEqualToView(bg)!.widthIs(125)!.heightIs(35)
       let _ =  priceBtn.titleLabel?.sd_layout()?.leftSpaceToView(priceBtn,0)!.heightIs(20)!.widthIs(45)!.centerYEqualToView(priceBtn)
        

        let sum1 = self.curStock!.adultPrice * Float(self.num1)
        let sum2 = self.curStock!.childPrice * Float(self.num2)

                   
        let myprilb = UILabel()
       priceBtn.addSubview(myprilb)
       myprilb.font = UIFont.commonFont
       myprilb.textColor = UIColor.GP_Orange_Color
        myprilb.text = String(format: "￥%.2f", sum1+sum2)
       let _  =  myprilb.sd_layout()?.leftSpaceToView(priceBtn,40)!.heightIs(20)!.centerYEqualToView(priceBtn)
       myprilb.setSingleLineAutoResizeWithMaxWidth(155)
   
       let icon = UIImageView()
       icon.image = UIImage(named: "向下")
       priceBtn.addSubview(icon)
       let _  =  icon.sd_layout()?.widthIs(16)!.heightIs(16)!.leftSpaceToView(self.priceLab,3)!.bottomSpaceToView(priceBtn,8)
                   

    }
    
    @objc func infoHideClick(){
        
        
        self.pricebg.removeFromSuperview()
    }

}
extension CalendarViewController:FSCalendarDelegate,FSCalendarDataSource{
    
    
    func calendar(_ calendar: FSCalendar, shouldSelect date: Date, at monthPosition: FSCalendarMonthPosition) -> Bool {
        
        return true
    }
    
    func calendar(_ calendar: FSCalendar, didSelect date: Date, at monthPosition: FSCalendarMonthPosition) {
        
        if monthPosition == .next || monthPosition == .previous{
            
            
            calendar.setCurrentPage(date, animated: true)
            
        }
        let dateString = self.dateFormatter?.string(from: date)
        let stock = self.prices![dateString!]
        self.curStock = stock

        if self.curStock == nil {
            
            ToastUtil.show(msg: "该日期没有对应的价格")
            
        }else{
            

            //self.dateClick!(stock!)
            self.updatePrice()
        }
        

        
    }
    func calendarCurrentPageDidChange(_ calendar: FSCalendar) {
        
        
    }
    func calendar(_ calendar: FSCalendar, boundingRectWillChange bounds: CGRect, animated: Bool) {
        
        calendar.frame = CGRect(origin: calendar.frame.origin, size: bounds.size)
    }
    
    func minimumDate(for calendar: FSCalendar) -> Date {
        
        return Date()
    }
    func maximumDate(for calendar: FSCalendar) -> Date {
        
        return Date().addingTimeInterval(90*24*3600)
    }
    
    func calendar(_ calendar: FSCalendar, subtitleFor date: Date) -> String? {
        
        let dateString = self.dateFormatter?.string(from: date)
        
        let stock = self.tempStocks[dateString!]
        
        return stock?.markPrice
    }
    
}
